package com.yw.thesimpllestplayer.ui;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Surface;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.yw.thesimpllestplayer.BaseActivity;
import com.yw.thesimpllestplayer.R;
import com.yw.thesimpllestplayer.mediaplayer.decoder.AudioDecoder;
import com.yw.thesimpllestplayer.mediaplayer.decoder.VideoDecoder;
import com.yw.thesimpllestplayer.renderview.RenderGLSurfaceView;
import com.yw.thesimpllestplayer.renderview.VideoDrawer;
import com.yw.thesimpllestplayer.renderview.VideoRender;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ProjectName: TheSimpllestplayer
 * @Package: com.yw.thesimpllestplayer.ui
 * @ClassName: SimplePlayerActivity
 * @Description: 简单的视频播放器
 * @Author: wei.yang
 * @CreateDate: 2021/11/6 15:59
 * @UpdateUser: 更新者：wei.yang
 * @UpdateDate: 2021/11/6 15:59
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class SimplePlayerActivity extends BaseActivity {
    private RenderGLSurfaceView surfaceView;
    private String videoPath = "/storage/emulated/0/DCIM/Camera/share_06909c9c4e74c23ff62eba7701038719.mp4";

    private VideoDecoder videoDecoder;
    private AudioDecoder audioDecoder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_player);
        initViews();
    }

    private void initViews() {
        surfaceView = findViewById(R.id.surfaceView);
        findViewById(R.id.btnChoiceVideo).setOnClickListener(v -> {
            choiceVideo();
        });
        findViewById(R.id.btnPlay).setOnClickListener(v -> {
            if (videoPath != null && videoDecoder != null && audioDecoder != null) {
                //开始播放
                videoDecoder.goOn();
                audioDecoder.goOn();
            } else {
                Toast.makeText(SimplePlayerActivity.this, "请先初始化播放器", Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.btnStop).setOnClickListener(v -> {
            if (videoPath != null && videoDecoder != null && audioDecoder != null) {
                //暂停播放
                videoDecoder.pause();
                audioDecoder.pause();
            } else {
                Toast.makeText(SimplePlayerActivity.this, "请先初始化播放器", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void videoPathCallback(String videoPath) {
        this.videoPath = videoPath;
        initPlayer();
    }


    private void initPlayer() {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        videoDecoder = new VideoDecoder(videoPath, new Surface(surfaceView.getVideoDrawer().getSurfaceTexture()));
        threadPool.execute(videoDecoder);

        audioDecoder = new AudioDecoder(videoPath);
        threadPool.execute(audioDecoder);

        //开始播放
//        videoDecoder.goOn();
//        audioDecoder.goOn();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (videoDecoder != null && audioDecoder != null) {
            videoDecoder.goOn();
            audioDecoder.goOn();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoDecoder != null && audioDecoder != null) {
            videoDecoder.pause();
            audioDecoder.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoDecoder != null) {
            videoDecoder.stop();
        }
        if (audioDecoder != null) {
            audioDecoder.stop();
        }
    }
}
